﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

namespace mprsc
{
    public partial class AddNewLoanPhone : Form
    {
        TblPhoneBranDAO BrandDAO = new TblPhoneBranDAO(Global.CONNECTION_STRING);
        TblModelDAO modelDAO = new TblModelDAO(Global.CONNECTION_STRING);
        TblLoan2PhoneDAO loan2PhoneDAO = new TblLoan2PhoneDAO(Global.CONNECTION_STRING);

        public AddNewLoanPhone()
        {
            InitializeComponent();
            LoadBrand();
            //LoadModel();
            AddMethod();
        }

        private void AddNewLoanPhone_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
          
        }
        //Add method
        private void AddMethod()
        {
            var method = new Dictionary<int, string>();
            method.Add(1, "Cash");
            method.Add(2, "EFT");
            cbMethod.DataSource = new BindingSource(method, null);
            cbMethod.DisplayMember = "Value";
            cbMethod.ValueMember = "Value";
        }
        // load Brand
        private void LoadBrand()
        {
            DataTable dt = BrandDAO.GetAll();
            cbBrand.DataSource = dt;
            cbBrand.DisplayMember = "BrandName";
            cbBrand.ValueMember = "IDBrand";
        }

        //load model all
        private void LoadModel()
        {
            cbModel.DataSource = modelDAO.GetModel();
            cbModel.ValueMember = "IDModel";
            cbModel.DisplayMember = "Model";
        }

        private void cbBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbBrand.SelectedValue.ToString() == "System.Data.DataRowView")
            {  
            }
            else
            {

                DataTable dt = modelDAO.GetModelByIdBrand(Convert.ToInt32(cbBrand.SelectedValue.ToString()));
                if (dt.Rows.Count >0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        cbModel.DataSource = dt;
                        cbModel.DisplayMember = "Model";
                        cbModel.ValueMember = "Model";
                    }
                }
                else
                {
                    MessageBox.Show("Not Find Model");
                }
            }      
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (cbModel.Text == "" || txt_Amount.Text == "" || txt_IMEI.Text == "" || txt_Amount.Text == "")
            {
                this.validationProvider1.ValidationMessages(!this.validationProvider1.Validate());
            }
            else
            {
                bool flag = false;
                try
                {
                    TblLoan2Phone loan2phone = new TblLoan2Phone();
                    loan2phone.LPhoneBrand = cbBrand.Text;
                    loan2phone.LPhoneModel = cbModel.Text;
                    loan2phone.LPIMENo = txt_IMEI.Text;
                    loan2phone.LoanStatus = 0;
                    loan2phone.LoanFee = Convert.ToDecimal(txt_LoanFree.Text);
                    loan2phone.Deposit = Convert.ToDecimal(txt_Amount.Text);
                    loan2phone.AccessoriresIncluded = txt_Included.Text;
                    loan2phone.Notes = txt_Notes.Text;

                    loan2phone.LPhoneID = "0";

                    if (txt_IMEI.Text.Trim().Length == 15)
                    {
                        loan2PhoneDAO.InsUpd_L2Phone(loan2phone, 1);
                        flag = true;
                    }
                    else
                    {
                       MessageBox.Show("Phone IMEI No must be enough 15 characters");
                       txt_IMEI.Focus();
                    }
                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                if (flag)
                {
                    MessageBox.Show("Save sucessful!");
                    this.Hide();
                    LoanPhone loanPhone = new LoanPhone();
                    loanPhone.Show();
                }
            }
        }

        private void txt_IMEI_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txt_Amount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txt_LoanFree_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
